/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#include "io/connection.h"
#include "io/message.h"
#include "netdll.h"

#include <qglobal.h>

#if defined Q_OS_LINUX
    #include <sys/socket.h>
#elif defined Q_OS_WIN32
    #include <winsock.h>
#elif defined Q_OS_WINCE
    #include <winsock.h>
#endif

#if defined Q_OS_LINUX
    typedef int SOCKET_TYPE;
    #define INVALID_SOCKET (-1)
#elif defined Q_OS_WIN32
    typedef SOCKET SOCKET_TYPE;
    typedef int socklen_t;
#elif defined Q_OS_WINCE
    typedef SOCKET SOCKET_TYPE;
    typedef int socklen_t;
#endif

namespace greenhills {
namespace net {

    /**
     * @brief This class represents message socket connection.
     *
     * @author Alexander Nikolskiy (alexander.nikolskiy@tieto.com)
     */
    class GH_NET_CLASS CMsgSocketConnection : public io::CConnection
    {
    public:
        /**
         * @brief Creates instance of this class and assigns specific socket
         * to it.
         *
         * @param pSocket socket number to assign to this connection.
         */
        CMsgSocketConnection(SOCKET_TYPE pSocket);

        /**
         * @brief Destructor.
         */
        virtual ~CMsgSocketConnection();

    public:
        /**
         * @brief Processes sending and receiving operations.
         */
        virtual void process();

        /**
         * @brief Closes this connection.
         */
        virtual void close();

        /**
         * @brief Returns connection state
         */
        virtual bool isOpened();

        virtual bool operator == (const io::CConnection &pConnection);

    protected:
        /**
         * @brief Holds number of socket assigned to this connection.
         */
        SOCKET_TYPE mSocket;

    private:
        /**
         * @brief Pointer to the currently receiving message.
         */

        io::CMessage mReceivingMessage;

        /**
         * @brief Holds length of the message that is currently being received.
         */
        unsigned long mReceivingMessageLength;

        /**
         * @brief Holds number of bytes received for this message.
         * Value should be between 0 and (mCurrentlyReceivingMessageLength
         * + sizeof(mCurrentlyReceivingMessageLength)).
         */
        unsigned long mBytesReceived;
    };

} //namespace net
} //namespace greenhills
